
package com.dps.omc.service.impl;
import com.dps.omc.model.AgentNode;
import com.dps.omc.model.JobArtifact;
import com.dps.omc.model.Task;
import com.dps.omc.model.TaskParam;
import com.dps.omc.strategy.StepManager;
import com.dps.common.utils.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dps.common.service.impl.AbstractPageService;
import com.dps.omc.dao.ITaskParamDAO;
import com.dps.omc.service.ITaskParamService;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
@Service("TaskParamServiceImpl")

public class TaskParamServiceImpl extends AbstractPageService<ITaskParamDAO,TaskParam> implements ITaskParamService {

	@Autowired
	private ITaskParamDAO taskParamDAO;
	@Autowired
	private StepManager stepManager;
	@Override
	public boolean getEnableDataPerm() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public ITaskParamDAO getDao() {
		return taskParamDAO;
	}

	@Override
	public void create(Task task, JobArtifact jobArtifact, AgentNode agentNode) {
		Map<String,String> data = new HashMap<String,String>();
		stepManager.execute(jobArtifact.getTemplateId(),agentNode,data);
		this.addon(data,task,jobArtifact);//增加额外参数
		for(String key:data.keySet())
		{
			String val = data.get(key);
			TaskParam taskParam = new TaskParam();
			taskParam.setId(IdGenerator.createNewId());
			taskParam.setTaskId(task.getId());
			taskParam.setNodeId(agentNode.getNodeId());
			taskParam.setTemplateId(jobArtifact.getTemplateId());
			taskParam.setTplKey(key);
			taskParam.setTplVal(val);
			this.insert(taskParam);
		}
	}

	private void addon(Map<String,String> data,Task task,JobArtifact jobArtifact)
	{
		data.put("artifactId",task.getArtifactId());
		data.put("artifactName",jobArtifact.getArtifactName());
		data.put("taskId",task.getId());
	}
}
